package com.tbit.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.tbit.common.entity.pojo.dto.OutAgeDTO;
import com.tbit.common.entity.pojo.vo.OutAgeVO;
import com.tbit.common.entity.view.Result;
import com.tbit.manager.api.OutAgeApi;
import com.tbit.uqbike.object.pojo.OutAge;
import com.tbit.uqbike.service.business.OutAgeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Objects;

/**
 * 一键停运openFeign 接口实现类
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-4-7
 */
@RestController
public class OutAgeApiImpl implements OutAgeApi {


    @Autowired
    private OutAgeService outAgeService;


    /**
     * 查询一键停运
     * @param brandId   品牌id
     * @param accountId 区域id
     * @return OutAge
     */
    @Override
    public OutAgeVO queryOutAge(Integer brandId, Integer accountId) {
        OutAgeVO outAgeVO = new OutAgeVO();
        OutAge data = outAgeService.queryOutAge(new OutAgeDTO(brandId, accountId)).getData();
        if (Objects.isNull(data)){
            return outAgeVO;
        }
        BeanUtils.copyProperties(data, outAgeVO);
        outAgeVO.setShowdownStartTime(Objects.isNull(data.getShowdownStartTime()) ? "" : DateUtil.format(data.getShowdownStartTime(), "yyyy-MM-dd"));
        outAgeVO.setShowdownEndTime(Objects.isNull(data.getShowdownEndTime()) ? "" : DateUtil.format(data.getShowdownEndTime(), "yyyy-MM-dd"));
        outAgeVO.setAnnouncementStartTime(Objects.isNull(data.getAnnouncementStartTime()) ? "" : DateUtil.format(data.getAnnouncementStartTime(), "yyyy-MM-dd HH:mm:ss"));
        outAgeVO.setAnnouncementEndTime(Objects.isNull(data.getAnnouncementEndTime()) ? "" : DateUtil.format(data.getAnnouncementEndTime(), "yyyy-MM-dd HH:mm:ss"));
        outAgeVO.setAddTime(Objects.isNull(data.getAddTime()) ? "" : DateUtil.format(data.getAddTime(), "yyyy-MM-dd HH:mm:ss"));
        outAgeVO.setUpdateTime(Objects.isNull(data.getUpdateTime()) ? "" : DateUtil.format(data.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
        return outAgeVO;
    }


    /**
     * 添加一键停运
     * @param outAgeVO outAgeVO
     * @return
     */
    @Override
    public Result<Object> addOrUpdate(OutAgeVO outAgeVO) {
        OutAge outAge = new OutAge();
        BeanUtils.copyProperties(outAgeVO, outAge);
        SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        outAge.setShowdownStartTime(StrUtil.isBlank(outAgeVO.getShowdownStartTime()) ? null : DateUtil.parse(outAgeVO.getShowdownStartTime(), day));
        outAge.setShowdownEndTime(StrUtil.isBlank(outAgeVO.getShowdownEndTime()) ? null : DateUtil.parse(outAgeVO.getShowdownEndTime(), day));
        outAge.setAnnouncementStartTime(StrUtil.isBlank(outAgeVO.getAnnouncementStartTime()) ? null : DateUtil.parse(outAgeVO.getAnnouncementStartTime(), sdf));
        outAge.setAnnouncementEndTime(StrUtil.isBlank(outAgeVO.getAnnouncementEndTime()) ? null : DateUtil.parse(outAgeVO.getAnnouncementEndTime(), sdf));
        return outAgeService.addOrUpdate(outAge);
    }
}
